/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.core.security;

import com.farmcode.api.plugin.AuthenticationPlugin;
import com.farmcode.api.plugin.ConfigureException;
import com.farmcode.api.plugin.PluginNotFountException;
import com.farmcode.api.security.AuthenticationAPI;
import com.farmcode.api.security.User;
import com.farmcode.api.util.FARMCodeAPI;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Local;
import javax.ejb.Stateless;

/**
 *
 * @author Oscar Ivan Prado
 */
@Local(AuthenticationAPI.class)
@Stateless(mappedName="ejb/authentication", name="ejb/Authentication")
public class AuthenticationBean implements AuthenticationAPI{

    @Override
    public User authenticate(String userName, String password) {
        User user = new User();
        boolean autenticated = false;
        user.setName(userName);
        try {
            AuthenticationPlugin plugin = FARMCodeAPI.getAuthenticationPlugin();
            autenticated = plugin.autheticate(userName, password);
        } catch (PluginNotFountException ex) {
            Logger.getLogger(AuthenticationBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(AuthenticationBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ConfigureException ex) {
            Logger.getLogger(AuthenticationBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        user.setAuthenticated(autenticated);
        return user;
    }
    
}
